package com.zmn.oms.business.impl.wechat;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.oms.business.interfaces.wechat.WechatDataMigrationBService;
import com.zmn.oms.model.entity.wechat.WechatMongo;
import com.zmn.oms.model.entity.wechat.WechatMongoQuery;
import com.zmn.oms.model.entity.wechatwork.OmsOrderWechatWorkLog;
import com.zmn.oms.services.interfaces.wechat.WechatMongoService;
import com.zmn.oms.services.interfaces.wechatwork.OmsOrderWechatWorkLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类描述: 迁移数据
 *
 * @author weilai
 * @since 2022/10/12  14:41
 */
@Service
@Slf4j
public class WechatDataMigrationBServiceImpl implements WechatDataMigrationBService {

    @Resource
    private WechatMongoService wechatMongoService;

    @Resource
    private OmsOrderWechatWorkLogService omsOrderWechatWorkLogService;

    @Override
    @Async
    public void wechatDataMigration() {
        try {
            log.info("迁移企业微信数据,异步开始");
            WechatMongoQuery wechatMongoQuery = new WechatMongoQuery();
            wechatMongoQuery.setPageSize(10000);
            int pageIndex = 1;
            while (true) {
                wechatMongoQuery.setPageIndex(pageIndex);
                List<WechatMongo> wechatMongos = wechatMongoService.listPageByQuery(wechatMongoQuery);
                if (CollectionUtil.isNullOrEmpty(wechatMongos)) {
                    break;
                }
                List<OmsOrderWechatWorkLog> omsOrderWechatWorkLogs = new ArrayList<>();
                for (WechatMongo wechatMongo : wechatMongos) {
                    OmsOrderWechatWorkLog omsOrderWechatWorkLog = new OmsOrderWechatWorkLog();
                    omsOrderWechatWorkLog.setOrderId(wechatMongo.getOrderId());
                    omsOrderWechatWorkLog.setWorkId(wechatMongo.getOrderId());
                    omsOrderWechatWorkLog.setNickName(wechatMongo.getWeixinName());
                    omsOrderWechatWorkLog.setZmnUserId(wechatMongo.getUserId());
                    omsOrderWechatWorkLog.setOperatorId(1L);
                    omsOrderWechatWorkLog.setOperatorType(1L);
                    omsOrderWechatWorkLog.setAddTime(new Date(wechatMongo.getCreateTime()));
                    omsOrderWechatWorkLog.setOperatingTime(new Date(wechatMongo.getCreateTime()));
                    omsOrderWechatWorkLog.setCreateTime(new Date(wechatMongo.getCreateTime()));
                    omsOrderWechatWorkLog.setUpdateTime(new Date(wechatMongo.getUpdateTime()));
                    omsOrderWechatWorkLogs.add(omsOrderWechatWorkLog);
                }
                omsOrderWechatWorkLogService.insertBatch(omsOrderWechatWorkLogs);
                pageIndex++;
                Thread.sleep(100);
            }
            log.info("迁移企业微信数据,异步结束");
        } catch (Exception e) {
            log.error("迁移企业微信数据,异步异常:{}", e);
        }

    }
}

